package com.tubang.logistics.controller;


import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubang.logistics.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.ResultData;
import com.tubang.component.util.RequestUtil;
import com.tubang.logistics.entity.InvoiceEntity;
import com.tubang.logistics.service.IInvoiceService;
import com.tubang.logistics.vo.InvoiceVo;

/**
 * <p>
 * 发票表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2017-12-18
 */
@RestController
@RequestMapping("/invoice")
public class InvoiceController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private IInvoiceService invoiceService;

	/**
	 * @param @return 设定文件
	 * @return ResultData 返回类型
	 * @throws
	 * @Title selectMessageList
	 * @Description 根据收发票用户id查询发票列表
	 */
	@GetMapping("selectInvoiceList")
	public ResultData selectInvoiceList(InvoiceVo invoiceVo, @RequestParam(required = true) String userId) {
		try {
			logger.info("[message]==> method[selectInvoiceList],请求数据为：[{}]", userId);
			InvoiceEntity invoiceEntity = BeanMapper.map(invoiceVo, InvoiceEntity.class);
			EntityWrapper<InvoiceEntity> ew = new EntityWrapper<>(invoiceEntity);
			Page<InvoiceEntity> page = new Page<>(invoiceVo.getPage(), invoiceVo.getRows());

			Page<InvoiceEntity> selectPage = invoiceService.selectPage(page, ew);
			int totalCount = invoiceService.selectCount(ew);

			logger.info("[message]==> method[selectInvoiceList],应答：[{}]", GsonQuick.getGson().toJson(selectPage));
			return ResultData.result(true).setTotalCount(totalCount).setData(selectPage.getRecords());
		} catch (Exception e) {
			logger.error("[message]==> method[selectInvoiceList],应答：[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}

	/**
	 * @return ResultData 返回类型
	 * @throws
	 * @Title insertOrUpdate
	 * @Description 发布或者修改发票
	 */
	@PostMapping("insertOrUpdate")
	public ResultData insertOrUpdate(HttpServletRequest request, HttpServletResponse response) {
		String receivePostJson = RequestUtil.receivePostJson(request);
		InvoiceEntity invoiceEntity = GsonQuick.toObject(receivePostJson, InvoiceEntity.class);
		try {
			logger.info("[message]==> method[insertOrUpdate],请求参数为：[{}]", GsonQuick.getGson().toJson(invoiceEntity));
			invoiceEntity.setUpdateTime(new Date());
			invoiceService.insertOrUpdate(invoiceEntity);
			logger.info("[message]==> method[insertOrUpdate],新增或者更新的发票数据为：[{}]", GsonQuick.getGson().toJson(invoiceEntity));
			return ResultData.result(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[message]==> method[insertOrUpdate],应答：[{}]", e.getMessage());
			return ResultData.result(false).setData(e);
		}
	}


	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listInvoice(
			@RequestParam String userId,
			@RequestParam boolean carrier,
			@RequestParam(required = false) Integer status,
			@RequestParam(required = false) String content,
			@RequestParam Integer page,
			@RequestParam Integer rows) {
		try {
			Map<String, Object> result = invoiceService.listInvoice(userId, carrier, status, content, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public @ResponseBody
	ResultData updateInvoice(
			@PathVariable String id,
			@RequestParam String url) {
		try {
			InvoiceEntity invoiceEntity = new InvoiceEntity();
			invoiceEntity.setId(id);
			invoiceEntity.setImg(url);
			invoiceEntity.setStatusCode(1);
			invoiceService.updateById(invoiceEntity);
			return ResultData.result(true).setMsg("更新成功");
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("更新异常");
		}
	}

}
